<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 14:35
 */

namespace HiCommon\Service;

use HiCommon\Repository\OrderCheckpointVehicleRepository;

class TaskQueueService extends BaseService
{
    /*定时任务队列执行*/

    /*用户车辆年检预约到期提醒-提前15天*/
    public function njyy_exec_user_vehicle_expire_remind(){
        $now_date = date('Y-m-d');
        $tomorrow_date = date('Y-m-d',strtotime('+1 day'));
        $error_table = 'error_njyy_remind';
        $now_table = 'njyy_remind_'.$now_date;
        $tomorrow_table = 'njyy_remind_'.$tomorrow_date;
        $arr_data = queue_out($now_table);

        if(count($arr_data) >= 1){
            foreach ($arr_data as $value)
            {
                $result = TaskQueueService::njyy_exec_remind_user($value);
                if($result['state'] == 0){
                    //成功
                    if($value['remind_day'] > 1){
                        //进入明天的队列，明天继续执行
                        $value['remind_day'] = intval($value['remind_day']) - 1;
                        queue_in($tomorrow_table,$value);
                    }
                    else{
                        //15次通知已结束
                    }
                }
                else{
                    //失败，进入错误队列，不再执行
                    $value['fail_num'] += 1;
                    $value['fail_msg'] = $result['msg'];
                    queue_in($error_table,$value);

                }
            }

        }

    }

    /*年检提醒*/
    public function njyy_exec_remind_user($data){
        try {
            $arr_where = ['order_sn'=>$data['order_sn']];
            $arr_option = [
              'with'=>['user_vehicle'],
              'field'=>['id','order_sn','users_vehicle_id','plate_no']

            ];
            $result = OrderCheckpointVehicleRepository::base_get_one($arr_where,$arr_option);
            if(is_null($result)) {
                return hello_error('数据不存在');
            }
            else{
                //成功
                $n_remind_day = intval($data['remind_day']) - 1;

                $arr_data_car       = $result->toarray();
                $arr_user_vehicle   = $arr_data_car['user_vehicle'];
                $arr_simple_vehicle = $arr_user_vehicle['simple_vehicle_model'];

                //查询订单信息
                $img                          = $arr_simple_vehicle['logo'];//车辆logo
                $title                        = '车辆年检提醒';
                $content                      = '您的车辆：' . $arr_data_car['plate_no'] . '即将过检，请尽快前往检测！';
                $user_id                      = $arr_user_vehicle['user_id'];
                $pid                          = 1;
                $order_id                     = $arr_data_car['id'];
                $extras                       = [];
                $message_extras               = [];
                $message_extras['order_type'] = 3;
                //                    $message_extras['main_type'] = $arr_order['main_type'];
                $message_extras['type']             = 14;  //【系统消息】车辆年检
                $message_extras['sys_type']         = 2;
                $message_extras['car_id']           = $arr_user_vehicle['id'];  //车辆id
                $message_extras['name']             = $arr_simple_vehicle['name'];  //
                $message_extras['one_name']         = $arr_simple_vehicle['one_name'];  //
                $message_extras['two_name']         = $arr_simple_vehicle['two_name'];  //
                $message_extras['three_name']       = $arr_simple_vehicle['three_name'];  //
                $message_extras['users_company_id'] = $arr_user_vehicle['users_company_id'];   //
                $message_extras['over_time']        = date('Y-m-d', time() + 3600 * 24 * $n_remind_day);  //   到期时间
                add_message($img, $title, $content, $user_id, $pid, $order_id, $extras, $message_extras);
                return hello_success('成功');


            }
        }
        catch (\Exception $exception) {
            return hello_warning('失败-'.$exception->getMessage(),-1);
        }

    }
}